package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbItemCatMapper;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.pojo.TbItemCatExample;
import com.dongdongshop.service.TicatService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

//分类管理表
@Service
@DubboService
public class TicatServiceimpl implements TicatService {


    @Autowired
    TbItemCatMapper tcm;

    Long ids= Long.valueOf(0);
    //查询全部
    @Override
    public List<TbItemCat> getll(Long id) {

        TbItemCatExample te=new TbItemCatExample();
        if(id !=null&&!"".equals(id)){
            TbItemCatExample.Criteria criteria = te.createCriteria();
            criteria.andParentIdEqualTo(id);
        }else if(id==null){
            TbItemCatExample.Criteria criteria = te.createCriteria();
            criteria.andParentIdEqualTo(ids);
        }
        List<TbItemCat> ls = tcm.selectByExample(te);
        return ls;


    }


    //增加回显
    @Override
    public TbItemCat addteat(Long id) {
        return tcm.selectByPrimaryKey(id);
    }


    //增加
    @Override
    public int addbaocun(TbItemCat tc) {
        return tcm.insertSelective(tc);
    }


    //修改回显
    @Override
    public TbItemCat updatecat(Long id) {
        return  tcm.selectByPrimaryKey(id);
    }

    //修改
    @Override
    public int updatebao(TbItemCat tc) {
        if(tc.getParentId()==null){
            Long ids= Long.valueOf(0);
            tc.setParentId(ids);
            tcm.updateByPrimaryKey(tc);
        }
        return tcm.updateByPrimaryKey(tc);
    }


    //删除
    @Override
    public int deletebyid(Long id) {
        return 0;
//        return tcm.deleteByPrimaryKey(id);
    }

    //根据主键查询
    @Override
    public TbItemCat byid(Long parentId) {
        return tcm.selectByPrimaryKey(parentId);
    }


    List<Long> list3=new ArrayList<>();
    //删除2删除父级
    @Override
    public int deletearr(List<Long> parentid) {


        for(int i=0;i<parentid.size();i++){
            list3.add(parentid.get(i));
        }

        delete(parentid);//调用递归方法
//        return tcm.deleteByPrimaryKey(parentid.get(i));

//        tbItemCatMapper.deleteByIds(lists);//根据所有的集合id进行删除。*/

        return 0;
    }


    public int delete(List<Long> parentid){//p为父id
        List <Long> list2=new ArrayList<>();

        for(int i=0;i<parentid.size();i++){
            List<TbItemCat> ls = tcm.selectbyparentid(parentid.get(i));//根据父id查询的子数据
            if (ls != null) {
                for (int j = 0; j < ls.size(); j++) {
                    Long id = ls.get(j).getId();
                    list2.add(id);
                }
            }
//            for(int y=0;y<list2.size();y++){//将list2的id加入总集合
//                list3.add(list2.get(y));
//                list2.clear();//清处list2
//            }

        }
//        tcm.deletebyparentid();
        return 1;
    }




    //三级联动查询
    @Override
    public List<TbItemCat> getsanji(Long parentId) {
        TbItemCatExample te=new TbItemCatExample();
        TbItemCatExample.Criteria criteria = te.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        return tcm.selectByExample(te);
    }

    //根据id查询此条记录
    @Override
    public TbItemCat getbyid(Long id) {
        return tcm.selectByPrimaryKey(id);
    }


}
